/*
 * Copyright 2010 jun.enomoto<jun.enomoto@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package foursquare4j.xml.handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import foursquare4j.type.Badge;
import foursquare4j.type.Badges;
import foursquare4j.type.CheckinResult;
import foursquare4j.type.Mayor;
import foursquare4j.type.Score;
import foursquare4j.type.Scoring;
import foursquare4j.type.Special;
import foursquare4j.type.Specials;
import foursquare4j.type.User;
import foursquare4j.type.Venue;

public class CheckinHandler extends Handler<CheckinResult> {

	private static enum E {
		_, VENUE, MAYOR, MAYOR_USER, BADGES, BADGES_BADGE, SCORING, SCORING_SCORE, SPECIALS, SPECIALS_SPECIAL, SPECIALS_SPECIAL_VENUE
	}

	private E e;

	private CheckinResult checkin;
	private Badge badge;
	private Score score;
	private Special special;

	@Override
	public void startDocument() throws SAXException {
		checkin = new CheckinResult();
	}

	@Override
	public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		if ("checkin".equals(qName)) e = E._;
		if ("venue".equals(qName) && e == E._) {
			e = E.VENUE;
			checkin.setVenue(new Venue());
		} else if ("mayor".equals(qName)) {
			e = E.MAYOR;
			checkin.setMayor(new Mayor());
		} else if ("user".equals(qName) && e == E.MAYOR) {
			e = E.MAYOR_USER;
			checkin.getMayor().setUser(new User());
		} else if ("badges".equals(qName)) {
			e = E.BADGES;
			checkin.setBadges(new Badges());
		} else if ("badge".equals(qName)) {
			e = E.BADGES_BADGE;
			checkin.getBadges().add(badge = new Badge());
		} else if ("scoring".equals(qName)) {
			e = E.SCORING;
			checkin.setScoring(new Scoring());
		} else if ("score".equals(qName)) {
			e = E.SCORING_SCORE;
			checkin.getScoring().add(score = new Score());
		} else if ("specials".equals(qName)) {
			e = E.SPECIALS;
			checkin.setSpecials(new Specials());
		} else if ("special".equals(qName)) {
			e = E.SPECIALS_SPECIAL;
			checkin.getSpecials().add(special = new Special());
		} else if ("venue".equals(qName) && e == E.SPECIALS_SPECIAL) {
			e = E.SPECIALS_SPECIAL_VENUE;
			special.setVenue(new Venue());
		}
	}

	@Override
	public void endElement(final String uri, final String localName, final String qName) throws SAXException {
		if ("checkin".equals(qName) && e == E._) e = null;
		else if ("venue".equals(qName) && e == E.VENUE) e = E._;
		else if ("mayor".equals(qName) && e == E.MAYOR) e = E._;
		else if ("user".equals(qName) && e == E.MAYOR_USER) e = E.MAYOR;
		else if ("badges".equals(qName) && e == E.BADGES) e = E._;
		else if ("badge".equals(qName) && e == E.BADGES_BADGE) e = E.BADGES;
		else if ("scoring".equals(qName) && e == E.SCORING) e = E._;
		else if ("score".equals(qName) && e == E.SCORING_SCORE) e = E.SCORING;
		else if ("specials".equals(qName) && e == E.SPECIALS) e = E._;
		else if ("special".equals(qName) && e == E.SPECIALS_SPECIAL) e = E.SPECIALS;
		else if ("venue".equals(qName) && e == E.SPECIALS_SPECIAL_VENUE) e = E.SPECIALS_SPECIAL;
	}

	@Override
	protected void characters(final String text) throws SAXException {
		switch (e) {
			case _:
				set(checkin, tag, text);
				break;
			case VENUE:
				set(checkin.getVenue(), tag, text);
				break;
			case MAYOR:
				set(checkin.getMayor(), tag, text);
				break;
			case MAYOR_USER:
				set(checkin.getMayor().getUser(), tag, text);
				break;
			case BADGES:
				break;
			case BADGES_BADGE:
				set(badge, tag, text);
				break;
			case SCORING:
				break;
			case SCORING_SCORE:
				set(score, tag, text);
				break;
			case SPECIALS:
				break;
			case SPECIALS_SPECIAL:
				set(special, tag, text);
				break;
			case SPECIALS_SPECIAL_VENUE:
				set(special.getVenue(), tag, text);
				break;
		}
	}

	@Override
	public CheckinResult getObject() {
		return checkin;
	}
}